home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / prolog_2.zip / PUZZLES.ZIP / CONSULTF.PRO < prev    next >
Text File  |  1986-07-20  |  1KB  |  49 lines

  1. /* This little program is the "consult" predicate rewritten in Prolog. 
  2. More precisely, it is the "conscl" predicate, since it does none of the
  3. fancy stuff that the "consult" allows in types FS and higher. The filename
  4. to be consulted is not automatically given the ".pro" extension like the 
  5. built-in consult, so you must remember that the name of the consulted file
  6. is taken to be exactly what you give.
  7.  
  8. When end-of-file is reached, "read" fails, so that X in the following
  9. clause is left a variable, Thus "asertfile succeeds only when end of
  10. file is reached. */
  11.  
  12. cons( Filename ) :-
  13.                    see( Filename ),
  14.                 repeat,
  15.         doread( X ),
  16.                 assertclause( X ),
  17.                 var( X ),
  18.                 seen( Filename ),
  19.                 see( user ),
  20.         !.     
  21.  
  22.     
  23. doread( X ) :- read( X ), !.
  24. doread( _ ).
  25.  
  26. assertclause(X) :- assertz(X), !.
  27. assertclause(_).
  28.  
  29.  
  30. /* The below method may be used with types FS and greater. It opens a file
  31. in the random access mode, rather than using the "see" predicate. */
  32.  
  33. consf( Filename ) :-
  34.                    open( Filename, r ),
  35.                 repeat,
  36.         doreadf( X, Filename ),
  37.                 assertclause( X ),
  38.                 var( X ),
  39.                    close( Filename ), 
  40.         !.     
  41.  
  42.  
  43.     
  44. doreadf( X, Filename ) :- read( X, Filename ), !.
  45. doreadf( _, _ ).
  46.  
  47.  
  48.  
  49.